package cn.xyh.controller;


import cn.xyh.exception.MenuException;
import cn.xyh.model.Menu;
import cn.xyh.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜单
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private IMenuService menuServiceImpl;

    @RequestMapping("/getEntityList.do")
    public ModelAndView getMenuList(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "sortField", required = false) String sortField,
                                    @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                    @ModelAttribute Menu entity
    ) {

        List<Menu> dataList = null;
        ModelAndView mv = null;
        try {
            dataList = menuServiceImpl.getMenuList(entity);
            mv = new ModelAndView("system-category");
            mv.addObject("dataList", dataList);
        } catch (MenuException e) {
            e.printStackTrace();
            logger.error("getEntityList{}--------------------------" + e.getMessage());
        }
        return mv;
    }

    /**
     * 新增
     *
     * @param request
     * @param response
     * @param entity
     * @return ResponseEntity
     */
    @RequestMapping("/insertMenu.do")
    public String insertMenu(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Menu entity
    ) {
        Integer result = 0;
        try {
            result = menuServiceImpl.insertMenu(entity);

        } catch (MenuException e) {
            logger.error(this.getClass().getName() + e.getMessage() + "出错了", "异常信息:" + e.toString());
            e.printStackTrace();

        }
        return null;
    }
}

